/*******************************************************************************
 * Copyright (c) 2014-8-18 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.foreveross.project.projectonline.service;

import java.util.Date;
import java.util.List;

import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.iff.infra.util.MD5Helper;

import com.foreveross.project.projectonline.model.TpMgtUser;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-8-18
 */
@SuppressWarnings("unchecked")
@Named("mgtUserService")
public class MgtUserService {

	String passwordMd5(String password) {
		return MD5Helper.string2MD5(MD5Helper.string2MD5(password));
	}

	public TpMgtUser login(String loginEmail, String password) {
		if (StringUtils.isBlank(loginEmail) || StringUtils.isBlank(password)) {
			return null;
		}
		TpMgtUser user = TpMgtUser.dao.findFirst("select * from TP_MGT_USER where LOGIN_EMAIL=? and IS_ENABLE='Y'",
				loginEmail);
		if (user == null) {
			return null;
		}
		System.out.println(passwordMd5("123456"));
		if (passwordMd5(password).equals(user.get("LOGIN_PASSWD"))) {
			return user;
		}
		return null;
	}
	
	public int changePasswrod(TpMgtUser user, String oldPasswrod, String newPassword) {
		if (StringUtils.isBlank(oldPasswrod) || StringUtils.isBlank(newPassword)) {
			return 0;
		}
		if (user == null) {
			return 2;
		}
		if (passwordMd5(oldPasswrod).equals(user.get("LOGIN_PASSWD"))) {
			boolean b =  user.set("LOGIN_PASSWD", passwordMd5(newPassword)).update();
			if(b)
				return 1;
			else
				return 0;
			
		} else
			return 3;
	}
	
	public List getUserList(String loginEmail) {
		if(StringUtils.isBlank(loginEmail))
			loginEmail = "";
		List<TpMgtUser> userList = TpMgtUser.dao.find("select * from TP_MGT_USER where LOGIN_EMAIL like ? ", "%" + loginEmail + "%");
		
		return userList;
	}
	
	public boolean addUser(String name, String password, String email, String desc, String state){
		if( !TpMgtUser.dao.findFirst("SELECT COUNT(ID) FROM TP_MGT_USER WHERE LOGIN_EMAIL = ?", email).get("COUNT(ID)").toString().equals("0"))
			return false;
		
		TpMgtUser user = new TpMgtUser()
						//.set("ID", id)
						.set("CATEGORY", "USER")
						.set("TYPE", "USER")
						.set("NAME", name)
						.set("LOGIN_EMAIL", email)
						.set("LOGIN_PASSWD", passwordMd5(password))
						.set("DESCIPTION", desc)
						//.set("CREATE_TIME", new Date())
						//.set("UPDATE_TIME", new Date())
						.set("IS_ENABLE", state);
		
		return user.save();
		
	}
	
	public int editUser(String id, String oldEmail, String name, String password, String email, String desc, String state){
		
		//当修改登录email时，需要检查是否已经存在该email了
		if(! oldEmail.equals(email)){
			if( !TpMgtUser.dao.findFirst("SELECT COUNT(ID) FROM TP_MGT_USER WHERE LOGIN_EMAIL = ?", email).get("COUNT(ID)").toString().equals("0"))
			return 0;
		}
		
		TpMgtUser user = getUser(id);
				user.set("NAME", name)
					.set("LOGIN_EMAIL", email)
					.set("LOGIN_PASSWD", passwordMd5(password))
					.set("DESCIPTION", desc)
					.set("IS_ENABLE", state);
		
		if(user.update()){
			return 1;
		} else
			return 2;
		
	}
	
	public boolean deleteUser(String id){
		return TpMgtUser.dao.deleteById(id);
	}
	
	public TpMgtUser getUser(String id){
		return TpMgtUser.dao.findById(id);
	}
	
}
